package com.bryantkobe.myspringcode.exception;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 失败响应记录类
 * @author: alienwarezzl
 * @date: 2025/9/22
 */
public record ErrorResult(int status,String errorCode,String message,
                          String timestamp,String path,
                          Map<String, Object> details) implements ResponseResult{

    public ErrorResult {
        timestamp = java.time.Instant.now().toString();
        details = details != null ? new ConcurrentHashMap<>(details) : new ConcurrentHashMap<>();
    }

    public static ErrorResult of(int status, String errorCode, String message, String path) {
        return new ErrorResult(status, errorCode, message,
                java.time.Instant.now().toString(), path, null);
    }

    /**
     * 添加详情信息
     */
    public ErrorResult withDetail(String key, Object value) {
        Map<String, Object> newDetails = new ConcurrentHashMap<>(this.details);
        newDetails.put(key, value);
        return new ErrorResult(status, errorCode, message, timestamp, path, newDetails);
    }

    public ErrorResult withParams(Map<String, Object> params) {
        return withDetail("params", params);
    }
}
